package com.ruoyi.system.service.impl;

import java.util.List;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.system.domain.LhkHotelInfo;
import com.ruoyi.system.domain.LhkReservation;
import com.ruoyi.system.mapper.LhkHotelInfoMapper;
import com.ruoyi.system.mapper.LhkReservationMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.LhkRoomMapper;
import com.ruoyi.system.domain.LhkRoom;
import com.ruoyi.system.service.ILhkRoomService;

/**
 * 房间信息管理Service业务层处理
 * 
 * @author fzb
 * @date 2025-05-29
 */
@Service
public class LhkRoomServiceImpl implements ILhkRoomService 
{
    @Autowired
    private LhkRoomMapper lhkRoomMapper;

    @Autowired
    private LhkHotelInfoMapper lhkHotelInfoMapper;

    @Autowired
    private LhkReservationMapper lhkReservationMapper;

    /**
     * 查询房间信息管理
     * 
     * @param roomId 房间信息管理主键
     * @return 房间信息管理
     */
    @Override
    public LhkRoom selectLhkRoomByRoomId(Long roomId)
    {
        return lhkRoomMapper.selectLhkRoomByRoomId(roomId);
    }

    /**
     * 查询房间信息管理列表
     * 
     * @param lhkRoom 房间信息管理
     * @return 房间信息管理
     */
    @Override
    public List<LhkRoom> selectLhkRoomList(LhkRoom lhkRoom)
    {

        List<LhkRoom> lhkRooms = lhkRoomMapper.selectLhkRoomList(lhkRoom);
        // 遍历所有房间信息
        /*for (LhkRoom room : lhkRooms) {
            // 如果房间的状态不为0，即非空闲状态，对退房时间进行判断，如果当前时间大于退房时间，则将房间状态改为0
            if (room.getStatus() != 0 && room.getStatus() != 3 && DateUtils.getNowDate().after(room.getCheckoutTime())) {
                LhkRoom updateRoom = new LhkRoom();
                updateRoom.setRoomId(room.getRoomId());
                updateRoom.setStatus(0L);
                updateRoom.setCheckinTime(null);
                updateRoom.setCheckoutTime(null);
                lhkRoomMapper.updateLhkRoom(updateRoom);
            }
        }*/
        return lhkRooms;
    }

    /**
     * 新增房间信息管理
     * 
     * @param lhkRoom 房间信息管理
     * @return 结果
     */
    @Override
    public int insertLhkRoom(LhkRoom lhkRoom)
    {
        lhkRoom.setCreateTime(DateUtils.getNowDate());
        int i = lhkRoomMapper.insertLhkRoom(lhkRoom);
        return i;
    }

    /**
     * 修改房间信息管理
     * 
     * @param lhkRoom 房间信息管理
     * @return 结果
     */
    @Override
    public int updateLhkRoom(LhkRoom lhkRoom)
    {
        if (lhkRoom == null){
            return 0;
        }
        if (lhkRoom.getStatus() == 2L) {
            // 修改房间状态为已入住
            // 根据房间id查询预约信息
            LhkReservation lhkReservation1 = new LhkReservation();
            lhkReservation1.setRoomId(lhkRoom.getRoomId());
            lhkReservation1.setHotelId(lhkRoom.getHotelId());
            // managerId为Long类型
            lhkReservation1.setManagerId(lhkRoom.getManagerId().longValue());
            System.out.println(lhkReservation1+"--------------------105");
            LhkReservation lhkReservation2 = lhkReservationMapper.selectLhkReservationByRoomId(lhkReservation1);
            System.out.println(lhkReservation2+"--------------------107");
            if (lhkReservation2 != null) {
                lhkReservation2.setStatus(2L);
                lhkReservation2.setCheckinDate(DateUtils.getNowDate());
                lhkReservationMapper.updateLhkReservation(lhkReservation2);
                lhkRoom.setCheckinTime(DateUtils.getNowDate()); // 入住时间为当前时间
            }
        } else if (lhkRoom.getStatus() == 0L) {
            // 修改房间状态为空闲
            // 根据房间id查询预约信息
            LhkReservation lhkReservation1 = new LhkReservation();
            lhkReservation1.setRoomId(lhkRoom.getRoomId());
            lhkReservation1.setHotelId(lhkRoom.getHotelId());
            // managerId为Long类型
            lhkReservation1.setManagerId(lhkRoom.getManagerId().longValue());
            LhkReservation lhkReservation2 = lhkReservationMapper.selectLhkReservationByRoomId(lhkReservation1);
            System.out.println(lhkReservation2+"--------------------123");
            if (lhkReservation2 != null) {
                lhkReservation2.setCheckoutDate(DateUtils.getNowDate());
                lhkReservationMapper.updateLhkReservation(lhkReservation2);
            }
            lhkRoom.setCheckinTime(null);
            lhkRoom.setCheckoutTime(null);
        }
        lhkRoom.setUpdateTime(DateUtils.getNowDate());
        int i = lhkRoomMapper.updateLhkRoom(lhkRoom);
        return i;
    }

    /**
     * 批量删除房间信息管理
     * 
     * @param roomIds 需要删除的房间信息管理主键
     * @return 结果
     */
    @Override
    public int deleteLhkRoomByRoomIds(Long[] roomIds)
    {
        return lhkRoomMapper.deleteLhkRoomByRoomIds(roomIds);
    }

    /**
     * 删除房间信息管理信息
     * 
     * @param roomId 房间信息管理主键
     * @return 结果
     */
    @Override
    public int deleteLhkRoomByRoomId(Long roomId)
    {
        return lhkRoomMapper.deleteLhkRoomByRoomId(roomId);
    }
}
